/**
 * 权限
 * WangPeixi
 */
var id = "";
var arr = [];
var parent="";
$(function(){
	id = request.QueryString("id");//从跳转页面过来的
	initCombobox();
	loadInfo();
	tree();
	$("#tt").tree("expandAll");
	$("#btncancel").click(function(){
		$("#updateForm").form('reset');
		$("#updateForm").form("disableValidation");
	});
	$("#updateForm").form("disableValidation");
	
});

function initCombobox(){
	// 初始化状态下拉菜单
	$('#type').combobox({
		editable : false, // 不可编辑状态
		cache : false,
		panelHeight : 'auto',// 自动高度适合
		valueField : 'code',
		textField : 'name'
	});
	// 数据放入下拉菜单
	var ut = postAsyncAjax("/findCodebySortCode?sortCode=006", null);// 获取状态
	if(ut.data == null)ut.data=[];
	ut.data.unshift({"code":"","name":"--请选择--"});
	$("#type").combobox("clear");
	$("#type").combobox("loadData",ut.data);
}

//回显信息
function loadInfo(){  
	 var data = new Object();
	 data.id=id;
	 var result = postAsyncAjax('/findLimitsById',data);
	 parent= result.data.parentCode; 
	 $("#updateForm").form('load',{
			id:result.data.id,
			name:result.data.name,
			icon:result.data.icon,
			type:result.data.type,
			addr:result.data.addr,
			code:result.data.code,
			sort:result.data.sort,
			parentCode:result.data.parentCode
		});
}
/**
 * 刷新grid
 */
function reloadGird() {
	gridReload("limitsList");
}


function updateMerchant(){
	var parentCode = $('#parentCode').datebox("getValue");
	
	if($("#updateForm").form('enableValidation')){
		$("#updateForm").form('submit',{
			url:'/updateLimits?parentCode='+parentCode,
			success:function(msg){
				msg = $.parseJSON(msg);
				if(msg.returnCode == '00'){
					 top.showMessage("修改成功,请手动刷新！");
					 closeIframeWindow();
					 reloadGird();
					 
				}else{
					top.showMessage("<font color='red'>" + msg.returnMsg  + "</font>!");
				}	
			}
		});
	}
}


function tree(){

	var t = 1;
	var param = [];
	var limit = postAsyncAjax("/findLimitsByParentCode?parentCode=-1").data;
	for (var i = 0; i < limit.length; i++) {
		param.push({
			"id" : limit[i].code,
			"text" : limit[i].name,
			"state" : "closed",
			"iconCls" : "icon-group",
			"t":t
		})
	}

	$("#parentCode").combotree({
		data : param,
		checkbox : false,
		cascadeCheck : false,
		required: true,  
		lines : true,
		onBeforeExpand : function(note) {
			if (note.t == 1) {
				userTreeLoad(note);
			}
		}
	});

	function userTreeLoad(note) {
		var param = new Array();
		var limit = postAsyncAjax("/findLimitsByParentCode?parentCode="+ note.id).data;
		for (var i = 0; i < limit.length; i++) {
			param.push({
				"id" : limit[i].code,
				"text" : limit[i].name,
				"state" : "closed",
				"t":t
			})
		}
		$('#parentCode').combotree('tree').tree('append',{
			parent : note.target,
			data : param
		});
		note.t = 2;
	}
	$('#parentCode').combotree('setValue',parent);
	

}